#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <chrono>
#include <thread>
#include <set>
#include <map>
#include <sw/redis++/redis++.h>

using namespace std;
using namespace sw::redis;

// zadd, zrange and zcard
void Test1(Redis& redis)
{
    redis.flushall();
    cout << "zadd, zrange and zcard" << endl;

    redis.zadd("key", "吕布", 99);
    redis.zadd("key", {
        make_pair("典韦", 98),
        make_pair("赵云", 97)
    });

    // zrange 是通过容器来区分带不带分数的
    vector<string> ret1;
    auto it1 = back_inserter(ret1);
    redis.zrange("key", 0, -1, it1);
    for(auto& e : ret1) cout << e << " ";
    cout << endl; 

    vector<pair<string, double>> ret2;
    auto it2 = back_inserter(ret2);
    redis.zrange("key", 0, -1, it2);
    for(auto& e : ret2) cout << e.first << " " << e.second << endl;

    auto len = redis.zcard("key");
    cout << "len: " << len << endl;
}

// zrem, zscore and zrange
void Test2(Redis& redis)
{
    redis.flushall();
    cout << "zrem, zscore and zrange" << endl;

    redis.zadd("key", "zhangsan", 90);
    redis.zadd("key", "lisi", 91);
    redis.zadd("key", "wangwu", 92);
    redis.zadd("key", "zhaoliu", 93);

    long long ret = redis.zrem("key", {"zhangsan", "lisi"});
    cout << "ret: " << ret << endl;

    auto len = redis.zcard("key");
    cout << "len: " << len << endl;

    auto score = redis.zscore("key", "wangwu");
    if(score)
        cout << "score: " << score.value() << endl;
    else
        cout << "score not exists" << endl;
    
    auto rank = redis.zrank("key", "wangwu");
    if(rank)
        cout << "rank: " << rank.value() << endl;
    else   
        cout << "rank not exists" << endl;
} 



int main()
{
    sw::redis::Redis redis("tcp://127.0.0.1:6379");
    // Test1(redis);
    Test2(redis);

    return 0;
}